package com.ruoyi.pingan.fymodule.account.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * @author: wangy
 * @description 业务异常响应码
 * @date: 2022/5/9 16:45
 */
@Getter
@AllArgsConstructor
public enum CodeEnum {


    SUCCESS(200, "操作成功"),
    CREATED(201, "创建成功"),
    SERVER_ERROR(-1, "系统错误"),
    UNAUTHORIZED(401, "未授权"),
    INTERNAL_SERVER_ERROR(500, "系统异常");


    private Integer code;

    private String message;

    public Map<String, Object> toMap() {
        Map<String, Object> map = new HashMap<>();
        map.put("code", this.code);
        map.put("message", this.message);
        return map;
    }

    public static CodeEnum getCodeEnum(Integer code) {
        Optional optional = Arrays.stream(CodeEnum.values()).filter(e -> e.getCode().equals(code)).findFirst();
        if (optional.isPresent()) {
            return CodeEnum.valueOf(optional.get().toString());
        }
        return null;
    }
}
